import { singleton, container } from "src/common/services/di";
import { BehaviorSubject } from 'rxjs';
import { i18n } from 'src/i18n/index';

@singleton()
export class I18nService {
  private _I18n$ = new BehaviorSubject<any | null>(null);

  public getI18n(module: string | string[], lang: 'en' | 'zh' | ''): void {
    const templateLocalPackage = i18n(lang) || {};

    if(typeof module === 'string'){
      return Object.assign({}, templateLocalPackage[module] || {});
    }
    if(module instanceof Array){
      let result: any = {};
      module.forEach(key => {
        result = Object.assign(result, templateLocalPackage[key] || {});
      });
      return result;
    }
  }

  public I18n$(){
    return this._I18n$.asObservable();
  }
};

export const I18nServiceFactoy = () => {
  return container.resolve(I18nService);
};